//
//  XYDownloadController.m
//  XYSfxyxuSwift
//
//  Created by 许洋 on 2018/4/19.
//  Copyright © 2018年 许洋. All rights reserved.
//

#import "XYDownloadController.h"
#import "XYDownloadViewModel.h"
#import "XYDownloadTableViewCell.h"
#import <ZFDownload/ZFDownloadManager.h>

#define  DownloadManager  [ZFDownloadManager sharedDownloadManager]

@interface XYDownloadController ()<ZFDownloadDelegate>

@property (nonatomic,strong) XYDownloadViewModel *viewModel;

@property (atomic, strong ) NSMutableArray *downloadObjectArr;


@end

static NSString *const kXYDownloadTableViewCell = @"XYDownloadTableViewCell";

@implementation XYDownloadController
- (XYDownloadViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[XYDownloadViewModel alloc]init];
    }
    return _viewModel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.rowHeight = 150;
    DownloadManager.downloadDelegate = self;

}
- (void)initData {
    
    [DownloadManager startLoad];
//    NSMutableArray *downladed = DownloadManager.finishedlist;
//    NSMutableArray *downloading = DownloadManager.downinglist;
//    self.downloadObjectArr = @[].mutableCopy;
//    [self.downloadObjectArr addObject:downladed];
//    [self.downloadObjectArr addObject:downloading];
    [self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return DownloadManager.downinglist.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XYDownloadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kXYDownloadTableViewCell];
    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:kXYDownloadTableViewCell bundle:nil] forCellReuseIdentifier:kXYDownloadTableViewCell];
        cell = [tableView dequeueReusableCellWithIdentifier:kXYDownloadTableViewCell];
    }
    ZFHttpRequest *request = DownloadManager.downinglist[indexPath.row];
    ZFFileModel *fileInfo = [request.userInfo objectForKey:@"File"];
    cell.fileInfo = fileInfo;
//    XYDownloadModel *model = self.viewModel.dataSource[indexPath.row];
//    [cell bindingModel:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZFHttpRequest *request = DownloadManager.downinglist[indexPath.row];
    ZFFileModel *downFile = [request.userInfo objectForKey:@"File"];
    ZFDownloadManager *filedownmanage = [ZFDownloadManager sharedDownloadManager];
    if(downFile.downloadState == ZFDownloading) { //文件正在下载，点击之后暂停下载 有可能进入等待状态
        [filedownmanage stopRequest:request];
    } else {
        [filedownmanage resumeRequest:request];
    }
    [self initData];

}
// 开始下载
- (void)startDownload:(ZFHttpRequest *)request {
    NSLog(@"开始下载!");
}

// 下载中
- (void)updateCellProgress:(ZFHttpRequest *)request {
    ZFFileModel *fileInfo = [request.userInfo objectForKey:@"File"];
    [self performSelectorOnMainThread:@selector(updateCellOnMainThread:) withObject:fileInfo waitUntilDone:YES];
}

// 下载完成
- (void)finishedDownload:(ZFHttpRequest *)request {
    [self initData];
}

// 更新下载进度
- (void)updateCellOnMainThread:(ZFFileModel *)fileInfo {
    NSArray *cellArr = [self.tableView visibleCells];
    for (id obj in cellArr) {
        if([obj isKindOfClass:[XYDownloadTableViewCell class]]) {
            XYDownloadTableViewCell *cell = (XYDownloadTableViewCell *)obj;
            if([cell.fileInfo.fileURL isEqualToString:fileInfo.fileURL]) {
                cell.fileInfo = fileInfo;
            }
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
